<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-quopri.html" />
<link rel="prev" href="module-binhex.html" />
<link rel="parent" href="netdata.html" />
<link rel="next" href="module-quopri.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>7.13 binascii -- Convert between binary and ASCII</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.12.1 Notes"
  href="binhex-notes.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7. internet Data Handling"
  href="netdata.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.14 quopri  "
  href="module-quopri.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="binhex-notes.html">7.12.1 Notes</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="netdata.html">7. Internet Data Handling</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-quopri.html">7.14 quopri  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0091300000000000000000">
7.13 <tt class="module">binascii</tt> --
         Convert between binary and ASCII</a>
</h1>

<p>
<a name="module-binascii"></a>

<p>
The <tt class="module">binascii</tt> module contains a number of methods to convert
between binary and various ASCII-encoded binary
representations. Normally, you will not use these functions directly
but use wrapper modules like <tt class="module"><a href="module-uu.html">uu</a></tt><a id='l2h-1634' xml:id='l2h-1634'></a>,
<tt class="module"><a href="module-base64.html">base64</a></tt><a id='l2h-1635' xml:id='l2h-1635'></a>, or
<tt class="module"><a href="module-binhex.html">binhex</a></tt><a id='l2h-1636' xml:id='l2h-1636'></a> instead. The <tt class="module">binascii</tt> module
contains low-level functions written in C for greater speed
that are used by the higher-level modules.

<p>
The <tt class="module">binascii</tt> module defines the following functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1616' xml:id='l2h-1616' class="function">a2b_uu</tt></b>(</nobr></td>
  <td><var>string</var>)</td></tr></table></dt>
<dd>
Convert a single line of uuencoded data back to binary and return the
binary data. Lines normally contain 45 (binary) bytes, except for the
last line. Line data may be followed by whitespace.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1617' xml:id='l2h-1617' class="function">b2a_uu</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Convert binary data to a line of ASCII characters, the return value
is the converted line, including a newline char. The length of
<var>data</var> should be at most 45.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1618' xml:id='l2h-1618' class="function">a2b_base64</tt></b>(</nobr></td>
  <td><var>string</var>)</td></tr></table></dt>
<dd>
Convert a block of base64 data back to binary and return the
binary data. More than one line may be passed at a time.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1619' xml:id='l2h-1619' class="function">b2a_base64</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Convert binary data to a line of ASCII characters in base64 coding.
The return value is the converted line, including a newline char.
The length of <var>data</var> should be at most 57 to adhere to the base64
standard.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1620' xml:id='l2h-1620' class="function">a2b_qp</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, header</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Convert a block of quoted-printable data back to binary and return the
binary data. More than one line may be passed at a time.
If the optional argument <var>header</var> is present and true, underscores
will be decoded as spaces.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1621' xml:id='l2h-1621' class="function">b2a_qp</tt></b>(</nobr></td>
  <td><var>data</var><big>[</big><var>, quotetabs, istext, header</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Convert binary data to a line(s) of ASCII characters in
quoted-printable encoding.  The return value is the converted line(s).
If the optional argument <var>quotetabs</var> is present and true, all tabs
and spaces will be encoded.  
If the optional argument <var>istext</var> is present and true,
newlines are not encoded but trailing whitespace will be encoded.
If the optional argument <var>header</var> is
present and true, spaces will be encoded as underscores per RFC1522.
If the optional argument <var>header</var> is present and false, newline
characters will be encoded as well; otherwise linefeed conversion might
corrupt the binary data stream.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1622' xml:id='l2h-1622' class="function">a2b_hqx</tt></b>(</nobr></td>
  <td><var>string</var>)</td></tr></table></dt>
<dd>
Convert binhex4 formatted ASCII data to binary, without doing
RLE-decompression. The string should contain a complete number of
binary bytes, or (in case of the last portion of the binhex4 data)
have the remaining bits zero.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1623' xml:id='l2h-1623' class="function">rledecode_hqx</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Perform RLE-decompression on the data, as per the binhex4
standard. The algorithm uses <code>0x90</code> after a byte as a repeat
indicator, followed by a count. A count of <code>0</code> specifies a byte
value of <code>0x90</code>. The routine returns the decompressed data,
unless data input data ends in an orphaned repeat indicator, in which
case the <tt class="exception">Incomplete</tt> exception is raised.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1624' xml:id='l2h-1624' class="function">rlecode_hqx</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Perform binhex4 style RLE-compression on <var>data</var> and return the
result.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1625' xml:id='l2h-1625' class="function">b2a_hqx</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Perform hexbin4 binary-to-ASCII translation and return the
resulting string. The argument should already be RLE-coded, and have a
length divisible by 3 (except possibly the last fragment).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1626' xml:id='l2h-1626' class="function">crc_hqx</tt></b>(</nobr></td>
  <td><var>data, crc</var>)</td></tr></table></dt>
<dd>
Compute the binhex4 crc value of <var>data</var>, starting with an initial
<var>crc</var> and returning the result.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1627' xml:id='l2h-1627' class="function">crc32</tt></b>(</nobr></td>
  <td><var>data</var><big>[</big><var>, crc</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compute CRC-32, the 32-bit checksum of data, starting with an initial
crc.  This is consistent with the ZIP file checksum.  Since the
algorithm is designed for use as a checksum algorithm, it is not
suitable for use as a general hash algorithm.  Use as follows:
<div class="verbatim"><pre>
    print binascii.crc32("hello world")
    # Or, in two pieces:
    crc = binascii.crc32("hello")
    crc = binascii.crc32(" world", crc)
    print crc
</pre></div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1628' xml:id='l2h-1628' class="function">b2a_hex</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt class="function">hexlify</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>Return the hexadecimal representation of the binary <var>data</var>.  Every
byte of <var>data</var> is converted into the corresponding 2-digit hex
representation.  The resulting string is therefore twice as long as
the length of <var>data</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1630' xml:id='l2h-1630' class="function">a2b_hex</tt></b>(</nobr></td>
  <td><var>hexstr</var>)</td></tr></table></dt>
<dd>
<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt class="function">unhexlify</tt></b>(</nobr></td>
  <td><var>hexstr</var>)</td></tr></table></dt>
<dd>Return the binary data represented by the hexadecimal string
<var>hexstr</var>.  This function is the inverse of <tt class="function">b2a_hex()</tt>.
<var>hexstr</var> must contain an even number of hexadecimal digits (which
can be upper or lower case), otherwise a <tt class="exception">TypeError</tt> is
raised.
</dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-1632' xml:id='l2h-1632' class="exception">Error</tt></b></dt>
<dd>
Exception raised on errors. These are usually programming errors.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-1633' xml:id='l2h-1633' class="exception">Incomplete</tt></b></dt>
<dd>
Exception raised on incomplete data. These are usually not programming
errors, but may be handled by reading a little more data and trying
again.
</dd></dl>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-base64.html">base64</a></tt>:</b>
    <dd>Support for base64 encoding used in MIME email messages.
  </dl>

<p>
<dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-binhex.html">binhex</a></tt>:</b>
    <dd>Support for the binhex format used on the Macintosh.
  </dl>

<p>
<dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-uu.html">uu</a></tt>:</b>
    <dd>Support for UU encoding used on <span class="Unix">Unix</span>.
  </dl>

<p>
<dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-quopri.html">quopri</a></tt>:</b>
    <dd>Support for quoted-printable encoding used in MIME email messages. .
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.12.1 Notes"
  href="binhex-notes.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7. internet Data Handling"
  href="netdata.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.14 quopri  "
  href="module-quopri.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="binhex-notes.html">7.12.1 Notes</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="netdata.html">7. Internet Data Handling</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-quopri.html">7.14 quopri  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
